package me.shurufa.widget.timelineheader;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import me.shurufa.R;
import me.shurufa.widget.animutils.AnimOnTouchListener;

/**
 * 作者 : andy
 * 日期 : 16/3/10 10:04
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 书摘Bar
 */
public class HeaderCommentBarLayout extends LinearLayout implements View.OnClickListener {

    public static final int INDEX_PRAISE  = 0;
    public static final int INDEX_TIME    = 1;
    public static final int INDEX_ME      = 2;

    LinearLayout ll_praise;
    LinearLayout ll_time;
    LinearLayout ll_me;

    TextView tv_praise;
    TextView tv_praise_num;
    TextView tv_time;
    TextView tv_me;

    ImageView iv_sort_time;

    // 存储状态
    private SparseBooleanArray mBoolArray = new SparseBooleanArray(1);
    private int mSelectedIndex = INDEX_PRAISE; // 默认选中最赞
    private OnBarItemClickListener mListener;

    public HeaderCommentBarLayout(Context context) {
        this(context, null);
    }

    public HeaderCommentBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderCommentBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public HeaderCommentBarLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View contentView = View.inflate(getContext(), R.layout.header_comment_bar, null);

        ll_praise = (LinearLayout) contentView.findViewById(R.id.ll_praise);
        ll_time = (LinearLayout) contentView.findViewById(R.id.ll_time);
        ll_me = (LinearLayout) contentView.findViewById(R.id.ll_me);

        tv_praise = (TextView) contentView.findViewById(R.id.tv_praise);
        tv_praise_num = (TextView) contentView.findViewById(R.id.tv_praise_num);
        tv_time = (TextView) contentView.findViewById(R.id.tv_time);
        tv_me = (TextView) contentView.findViewById(R.id.tv_me);

        iv_sort_time = (ImageView) contentView.findViewById(R.id.iv_sort_time);

        ll_praise.setOnClickListener(this);
        ll_time.setOnClickListener(this);
        ll_me.setOnClickListener(this);

        ll_praise.setOnTouchListener(new AnimOnTouchListener());
        ll_time.setOnTouchListener(new AnimOnTouchListener());
        ll_me.setOnTouchListener(new AnimOnTouchListener());
        addView(contentView);

        // 设置升序降序布尔值
        mBoolArray.put(INDEX_TIME, false);
        mBoolArray.put(INDEX_PRAISE, false);
        mBoolArray.put(INDEX_ME, false);

        // 默认选中第一项
        setSelected(INDEX_PRAISE);
    }

    private void changeTextStyleBySate(int selectedIndex) {
        mSelectedIndex = selectedIndex;
        int selectedColor = getContext().getResources().getColor(R.color.red_tab_text);
        int unSelectedColor = getContext().getResources().getColor(R.color.gray_tab_text);
        tv_praise.setTextColor(selectedIndex == INDEX_PRAISE ? selectedColor : unSelectedColor);
        tv_time.setTextColor(selectedIndex == INDEX_TIME ? selectedColor : unSelectedColor);
        tv_me.setTextColor(selectedIndex == INDEX_ME ? selectedColor : unSelectedColor);

        boolean timeSort = mBoolArray.get(INDEX_TIME);
        iv_sort_time.setImageResource(INDEX_TIME == selectedIndex
                ? (timeSort ? R.drawable.ic_sort_asc : R.drawable.ic_sort_desc)
                : R.drawable.ic_sort);
    }

    /**
     * 设置默认选中
     * @param index
     */
    public void setSelected(int index) {
        changeTextStyleBySate(index);
        if (mListener != null) {
            mListener.onClick(index, true, mSelectedIndex);
        }
    }

    public void hideMe() {
        ll_me.setVisibility(GONE);
    }

    /**
     * 获得当前选中的索引
     * @return
     */
    public int getCurrentIndex() {
        return mSelectedIndex;
    }


    /**
     * 获得当前索引 的排列方式
     * @return DESC = true  ASC: false
     */
    public boolean isDescOfCurrentItem() {
        return !mBoolArray.get(getCurrentIndex());
    }

    public void setOnBarItemClickListener(OnBarItemClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onClick(View v) {
        if (mListener == null) {
            return;
        }

        int curSelectedIndex = 0;
        switch (v.getId()) {
            case R.id.ll_praise:
                mListener.onClick(INDEX_PRAISE, true, mSelectedIndex);
                curSelectedIndex = INDEX_PRAISE;
                break;


            case R.id.ll_time:
                boolean newTimeState = false;  // 默认降序
                if (getCurrentIndex() == INDEX_TIME) {
                    newTimeState = !mBoolArray.get(INDEX_TIME);
                }
                mBoolArray.put(INDEX_TIME, newTimeState);
                mListener.onClick(INDEX_TIME, newTimeState, mSelectedIndex);
                curSelectedIndex = INDEX_TIME;
                break;


            case R.id.ll_me:
                mListener.onClick(INDEX_ME, true, mSelectedIndex);
                curSelectedIndex = INDEX_ME;
                break;
        }

        changeTextStyleBySate(curSelectedIndex);
    }

}
